package logic

import (
	"one-wiki/commons"
	"one-wiki/database"
)

func MoveOutGroup(session commons.Session, groupID int64, userID int64) {
	// todo 校验权限

	// 从组中移除用户
	err := database.DeleteUserGroup(userID, groupID)
	if err != nil {
		panic(err)
	}
}

func GroupDetail(session commons.Session, groupID int64) commons.UserGroupDetail {
	// 查询用户组信息
	group, err2 := database.GetGroupByID(groupID)
	if err2 != nil {
		panic(err2)
	}

	// 查询成员信息
	users, err2 := database.ListUsersByGroupID(groupID)
	if err2 != nil {
		panic(err2)
	}
	// 封装成员列表
	members := []commons.UserGroupMember{}

	for _, user := range users {
		member := commons.UserGroupMember{
			UserID:   user.ID,
			UserName: user.UserName,
			Avatar:   user.Avatar,
		}
		members = append(members, member)
	}

	return commons.UserGroupDetail{
		ID:        group.ID,
		CanUpdate: group.CreatorID == session.UserID || session.IsAdmin(),
		Name:      group.Name,
		Profile:   group.Profile,
		Members:   members,
	}
}
